<?php

namespace App\Models;

use App\Standard\CommonFun;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements JWTSubject
{
    use HasFactory, Notifiable;

    protected $connection = 'pms';

    protected $table = 'user';

    public function __construct(array $attributes = [])
    {
        $this->table ='pms.user';
        parent::__construct($attributes);
    }


    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];

    public function person()
    {
        return $this->hasOne(Person::class,'id','person_id');
    }

    public function report(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(ReportWork::class,'id','user_id')
            ->orderBy('report_date','desc');
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims() : array
    {
        return [];
    }

    //查询数据
    public static function lists($page = 1, $limit = 20){
        $where=[];
        $where[]=['username','!=','null'];
        $query=self::where($where);
        $count=$query->count();
        $data = $query->offset(\App\Libraries\Common\CommonFun::getOffset($page, $limit))->limit($limit)->orderBy('id', 'desc')->get()->map(function (User $rebate) use ($count) {
            $rebate = $rebate->attributesToArray();
            $rebate['name']=Person::query()->where('id',$rebate['person_id'])->first()->getAttribute('name');
            // $rebate['is_show_home'] = $rebate['is_show_home']==0 ?'否' : '是';

            return $rebate;
        })->toArray();
        return $data ? compact('count', 'data') : false;

    }


}
